package ms.core.gateway.request;

import java.util.List;
import java.util.Map;

import lombok.Data;
import ms.core.common.AppException;
import ms.core.common.consts.SysErr;
import ms.core.tool.CollectionTool;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;

@Data
public class UpdateFieldRequest {
	private String code;			// 表代码
	private List<Field> fields;		// 表字段
	
	@Data
	public static class Field {
		private String name;		// 字段名称
		private int width;			// 列宽度
		private String sname;		// 显示名
		private int showIdx;		// 显示顺序
	}
	
	public static UpdateFieldRequest getInstance(Map<String, Object> body) {
		UpdateFieldRequest req = JsonTool.objectToBean(body, UpdateFieldRequest.class);
		if (req==null)
			throw new AppException(SysErr.SYS_PARAM_ERR);

		if (StrTool.isBlank(req.getCode())) throw new AppException(SysErr.SYS_PARAM_ERR, "请输入需要设置表代码.");

		if (CollectionTool.isEmpty(req.getFields()))
			throw new AppException(SysErr.SYS_PARAM_ERR, "表字段列表不可空.");

		return req;
	}
}